//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.sale;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import org.jpgolay.gui.date.DateComboBox;
import org.jpgolay.gui.iconBar.*;
import org.jpgolay.me.list.ListPrint;
import org.jpgolay.me.order.OrderController;
import org.jpgolay.me.persistence.ObjectCRUD;
import org.jpgolay.me.piece.PiecesPrint;
import org.jpgolay.me.salesperson.SalesPerson;
import org.jpgolay.me.util.*;

// Referenced classes of package org.jpgolay.me.sale:
//            OLD_SaleSQLList
public class OLD_SaleListGUI extends JInternalFrame {

    private class TableModel extends AbstractTableModel {

        public int getColumnCount() {
            return colonnes.size();
        }

        public int getRowCount() {
            return rangees.size() / getColumnCount();
        }

        public String getColumnName(int col) {
            String colName = "";
            if (col <= getColumnCount()) {
                colName = (String) colonnes.elementAt(col);
            }
            return colName;
        }

        public Class getColumnClass(int col) {
            return getValueAt(0, col).getClass();
        }

        public boolean isCellEditable(int row, int col) {
            return false;
        }

        public Object getValueAt(int row, int col) {
            return (String) rangees.elementAt(row * getColumnCount() + col);
        }

        public void setValueAt(Object obj, int i, int j) {
        }

        TableModel() {
        }
    }

    class SharedListSelectionHandler
            implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel) e.getSource();
            if (!lsm.isSelectionEmpty()) {
                int minIndex = lsm.getMinSelectionIndex();
                int maxIndex = lsm.getMaxSelectionIndex();
                int tailleTableau = (maxIndex - minIndex) + 1;
                if (vtId != null) {
                    vtId = null;
                }
                vtId = new String[tailleTableau];
                int iTabl = 0;
                for (int i = minIndex; i <= maxIndex; i++) {
                    if (lsm.isSelectedIndex(i)) {
                        vtId[iTabl] = (String) table.getValueAt(i, 0);
                    }
                    iTabl++;
                }

            }
        }

        SharedListSelectionHandler() {
        }
    }

    public OLD_SaleListGUI() {
        dateDuAu = 0;
        dateDu = "";
        dateAu = "";
        typePiece = "DEVIS";
        vtId = null;
        initComponents();
        setFrameIcon(new ImageIcon(getClass().getResource("/org/jpgolay/me/images/pieces18x18.png")));
        fillComboBox();
        iconBar1.addAnswerListener(new AnswerListener() {
            public void create(AnswerEvent answerevent) {
            }

            public void open(AnswerEvent e) {
                ouvrir();
            }

            public void modify(AnswerEvent answerevent) {
            }

            public void save(AnswerEvent answerevent) {
            }

            public void remove(AnswerEvent answerevent) {
            }

            public void print(AnswerEvent e) {
                imprimer();
            }

            public void help(AnswerEvent e) {
                aide();
            }
        });
        dateAu = (new MEDate()).getPlain();
        dateDu = "01" + dateAu.substring(2);
        screen = Toolkit.getDefaultToolkit().getScreenSize();
    }

    private void initComponents() {
        buttonGroup1 = new ButtonGroup();
        iconBar1 = new IconBar();
        mainP = new JPanel();
        FlowLayout flowLayout3 = new FlowLayout();
        FlowLayout flowLayout8 = new FlowLayout();
        selectionP = new JPanel();
        fieldSelectionP = new JPanel();
        typePieceJCB = new JComboBox();
        agencesJCB = new JComboBox();
        parCommercialJR = new JRadioButton();
        commerciauxJCB = new JComboBox();
        parJR = new JRadioButton();
        selectJCB = new JComboBox();
        requeteF = new JTextField();
        periodeJCB = new JComboBox();
        duF = new DateComboBox();
        auL = new JLabel();
        auF = new DateComboBox();
        validerB = new JButton();
        dataP = new JPanel();
        defiler = new JScrollPane();
        voletBas = new JPanel();
        chiffreBrutJL = new JLabel();
        chiffreResteDuJL = new JLabel();
        chiffreNetJL = new JLabel();
        jMenuBar1 = new JMenuBar();
        optionsM = new JMenu();
        newMI = new JMenuItem();
        openMI = new JMenuItem();
        saveMI = new JMenuItem();
        modifyMI = new JMenuItem();
        removeMI = new JMenuItem();
        jSeparator1 = new JSeparator();
        printMI = new JMenuItem();
        helpMI = new JMenuItem();
        setTitle("LISTE VENTES");
        iconBar1.setEnabledCreate(false);
        iconBar1.setEnabledModify(false);
        iconBar1.setEnabledRemove(false);
        iconBar1.setEnabledSave(false);
        iconBar1.setPreferredSize(new Dimension(191, 29));
        getContentPane().add(iconBar1, "North");
        mainP.setLayout(new BorderLayout());
        mainP.setBorder(new BevelBorder(1));
        typePieceJCB.setModel(new DefaultComboBoxModel(new String[]{
                    "DEVIS", "COMMANDES", "BL", "FACTURES", "AVOIRS"
                }));
        agencesJCB.setModel(new DefaultComboBoxModel(new String[]{
                    "AGENCE 1", "AGENCE 2", "AGENCE 3", "AGENCE 4", "AGENCE 5", "TOUTES LES AGENCES"
                }));
        parCommercialJR.setText("par commercial");
        buttonGroup1.add(parCommercialJR);
        parCommercialJR.setHorizontalAlignment(4);
        parCommercialJR.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                parCommercialJRActionPerformed(evt);
            }
        });
        parJR.setSelected(true);
        parJR.setText("par");
        buttonGroup1.add(parJR);
        parJR.setHorizontalAlignment(4);
        parJR.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                parJRActionPerformed(evt);
            }
        });
        selectJCB.setModel(new DefaultComboBoxModel(new String[]{
                    "CLIENT", "CODE CLIENT", "NUMERO", "ECHEANCE", "RESTE DU"
                }));
        selectJCB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                selectJCBActionPerformed(evt);
            }
        });
        requeteF.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                requeteFActionPerformed(evt);
            }
        });
        periodeJCB.setModel(new DefaultComboBoxModel(new String[]{
                    "MOIS EN COURS", "ANNEE EN COURS", "DU", "TOUS"
                }));
        periodeJCB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                periodeJCBActionPerformed(evt);
            }
        });
        duF.setEnabled(false);
        auL.setHorizontalAlignment(0);
        auL.setText("AU");
        auF.setEnabled(false);
        validerB.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/me/images/search18x18.png")));
        validerB.setToolTipText("Lancer la recherche");
        validerB.setBorderPainted(false);
        validerB.setFocusPainted(false);
        validerB.setContentAreaFilled(false);
        validerB.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                validerBActionPerformed(evt);
            }
        });
        mainP.add(selectionP, "North");
        dataP.setLayout(new BorderLayout());
        dataP.add(defiler, "Center");
        voletBas.setLayout(new GridLayout(1, 3));
        fieldSelectionP.setLayout(flowLayout3);
        chiffreBrutJL.setHorizontalAlignment(0);
        voletBas.add(chiffreBrutJL);
        chiffreResteDuJL.setHorizontalAlignment(0);
        voletBas.add(chiffreResteDuJL);
        chiffreNetJL.setHorizontalAlignment(0);
        voletBas.add(chiffreNetJL);
        dataP.add(voletBas, "South");
        mainP.add(dataP, "Center");
        getContentPane().add(mainP, "Center");
        optionsM.setMnemonic('O');
        optionsM.setText("Options");
        newMI.setAccelerator(KeyStroke.getKeyStroke(78, 2));
        newMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/iconBar/images/new.png")));
        newMI.setText("Nouveau");
        newMI.setEnabled(false);
        optionsM.add(newMI);
        openMI.setAccelerator(KeyStroke.getKeyStroke(79, 2));
        openMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/iconBar/images/open.png")));
        openMI.setText("Ouvrir");
        openMI.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                openMIActionPerformed(evt);
            }
        });
        optionsM.add(openMI);
        saveMI.setAccelerator(KeyStroke.getKeyStroke(83, 2));
        saveMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/iconBar/images/save.png")));
        saveMI.setText("Enregistrer");
        saveMI.setEnabled(false);
        optionsM.add(saveMI);
        modifyMI.setAccelerator(KeyStroke.getKeyStroke(77, 2));
        modifyMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/iconBar/images/modify.png")));
        modifyMI.setText("Modifier");
        modifyMI.setEnabled(false);
        optionsM.add(modifyMI);
        removeMI.setAccelerator(KeyStroke.getKeyStroke(88, 2));
        removeMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/iconBar/images/delete.png")));
        removeMI.setText("Supprimer");
        removeMI.setEnabled(false);
        optionsM.add(removeMI);
        optionsM.add(jSeparator1);
        printMI.setAccelerator(KeyStroke.getKeyStroke(80, 2));
        printMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/iconBar/images/printer.png")));
        printMI.setText("Imprimer");
        printMI.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                printMIActionPerformed(evt);
            }
        });
        optionsM.add(printMI);
        helpMI.setAccelerator(KeyStroke.getKeyStroke(65, 2));
        helpMI.setIcon(new ImageIcon(getClass().getResource("/org/jpgolay/gui/iconBar/images/help.png")));
        helpMI.setText("Aide");
        helpMI.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                helpMIActionPerformed(evt);
            }
        });
        optionsM.add(helpMI);
        jMenuBar1.add(optionsM);
        setJMenuBar(jMenuBar1);
        fieldSelectionP.setPreferredSize(new Dimension(900, 54));
        typePieceJCB.setPreferredSize(new Dimension(150, 24));
        agencesJCB.setPreferredSize(new Dimension(100, 24));
        commerciauxJCB.setPreferredSize(new Dimension(150, 24));
        selectJCB.setPreferredSize(new Dimension(150, 24));
        requeteF.setPreferredSize(new Dimension(150, 24));
        periodeJCB.setPreferredSize(new Dimension(150, 24));
        duF.setPreferredSize(new Dimension(150, 24));
        auL.setPreferredSize(new Dimension(16, 24));
        auF.setPreferredSize(new Dimension(150, 24));
        validerB.setPreferredSize(new Dimension(24, 24));
        flowLayout3.setVgap(2);
        flowLayout3.setHgap(2);
        selectionP.setPreferredSize(new Dimension(900, 54));
        fieldSelectionP.add(typePieceJCB, null);
        fieldSelectionP.add(agencesJCB, null);
        fieldSelectionP.add(parCommercialJR, null);
        fieldSelectionP.add(commerciauxJCB, null);
        fieldSelectionP.add(parJR, null);
        fieldSelectionP.add(selectJCB, null);
        fieldSelectionP.add(requeteF, null);
        fieldSelectionP.add(validerB, null);
        fieldSelectionP.add(periodeJCB, null);
        fieldSelectionP.add(duF, null);
        fieldSelectionP.add(auL, null);
        fieldSelectionP.add(auF, null);
        selectionP.setLayout(flowLayout8);
        selectionP.add(fieldSelectionP, null);
        flowLayout8.setVgap(0);
        pack();
    }

    private void parJRActionPerformed(ActionEvent evt) {
        par();
    }

    private void parCommercialJRActionPerformed(ActionEvent evt) {
        parCommercial();
    }

    private void selectJCBActionPerformed(ActionEvent evt) {
        selectJCB();
    }

    private void periodeJCBActionPerformed(ActionEvent evt) {
        periodeJCB();
    }

    private void requeteFActionPerformed(ActionEvent evt) {
        valider();
    }

    private void validerBActionPerformed(ActionEvent evt) {
        valider();
    }

    private void openMIActionPerformed(ActionEvent evt) {
        ouvrir();
    }

    private void printMIActionPerformed(ActionEvent evt) {
        imprimer();
    }

    private void helpMIActionPerformed(ActionEvent evt) {
        aide();
    }

    private void fillComboBox() {
        commerciauxJCB.setModel(new DefaultComboBoxModel(getSalesPersonList()));
        commerciauxJCB.setEnabled(false);
    }

    private String[] getSalesPersonList() {
        if (crud == null) {
            crud = new ObjectCRUD();
        }
        java.util.Collection c = crud.readAll(null, 15);
        if (c != null) {
            SalesPerson salesPerson = null;
            ArrayList salesPersonList = new ArrayList(c);
            Iterator it = salesPersonList.iterator();
            String list[] = new String[salesPersonList.size()];
            commerciauxId = new int[salesPersonList.size()];
            for (int i = 0; it.hasNext(); i++) {
                salesPerson = (SalesPerson) it.next();
                commerciauxId[i] = salesPerson.getId().intValue();
                list[i] = salesPerson.getPrenom() + " " + salesPerson.getNom();
            }

            return list;
        } else {
            commerciauxId = null;
            return (new String[]{
                        "aucune"
                    });
        }
    }

    private void par() {
        commerciauxJCB.setEnabled(false);
        selectJCB.setEnabled(true);
        requeteF.setEditable(true);
    }

    private void parCommercial() {
        commerciauxJCB.setEnabled(true);
        selectJCB.setEnabled(false);
        requeteF.setEditable(false);
    }

    private void selectJCB() {
        if (selectJCB.getSelectedIndex() == 3 || selectJCB.getSelectedIndex() == 4) {
            requeteF.setText("");
            requeteF.setEditable(false);
        } else {
            requeteF.setText("");
            requeteF.setEditable(true);
        }
    }

    private void periodeJCB() {
        switch (periodeJCB.getSelectedIndex()) {
            case 0: // '\0'
                auF.setModel(new DefaultComboBoxModel(new String[]{
                            ""
                        }));
                auF.setEnabled(false);
                duF.setModel(new DefaultComboBoxModel(new String[]{
                            ""
                        }));
                duF.setEnabled(false);
                dateAu = (new MEDate()).getPlain();
                dateDu = "01" + dateAu.substring(2);
                break;

            case 2: // '\002'
                auF.setEnabled(true);
                duF.setEnabled(true);
                dateDu = "";
                dateAu = "";
                break;

            case 3: // '\003'
                auF.setModel(new DefaultComboBoxModel(new String[]{
                            ""
                        }));
                auF.setEnabled(false);
                duF.setModel(new DefaultComboBoxModel(new String[]{
                            ""
                        }));
                duF.setEnabled(false);
                dateDu = "01/01/1990";
                dateAu = "01/01/2090";
                break;

            case 1: // '\001'
                auF.setModel(new DefaultComboBoxModel(new String[]{
                            ""
                        }));
                auF.setEnabled(false);
                duF.setModel(new DefaultComboBoxModel(new String[]{
                            ""
                        }));
                duF.setEnabled(false);
                dateAu = "31/12" + (new MEDate()).getPlain().substring(5);
                dateDu = "01/01" + dateAu.substring(5);
                break;
        }
    }

    private void valider() {
        selectRequete();
    }

    private void ouvrir() {
    }

    private void imprimer() {
        if (ps == null) {
            ps = new PrintSelectorGUI();
            ps.getValiderB().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    if (ps.isSelected() == 0) {
                        print();
                    } else {
                        printPiece();
                    }
                    ps.setVisible(false);
                    ps = null;
                }
            });
            ps.getAnnulerB().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    ps.setVisible(false);
                    ps = null;
                }
            });
        }
        screen = getSize();
        ps.setBounds(screen.width / 2 - 100, screen.height / 2, 200, 120);
        ps.setVisible(true);
    }

    private void aide() {
        BrowserControl.displayURL("www.media-4-u.net");
    }

    private void getVentesByClient() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchBy(1, typePieceJCB.getSelectedIndex(), agencesJCB.getSelectedIndex() + 1, dateDu, dateAu)) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ " + typePiece + " PAR CLIENT ]");
        } else {
            JOptionPane.showMessageDialog(this, "Aucune donn\351e ne correspond \340 la requ\352te demand\351e !");
        }
    }

    private void getVentesByClientAllAG() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchBy(1, typePieceJCB.getSelectedIndex(), dateDu, dateAu)) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ " + typePiece + " PAR CLIENT ( TOUTES LES AGENCES ) ]");
        } else {
            JOptionPane.showMessageDialog(this, "Aucune donn\351e ne correspond \340 la requ\352te demand\351e !");
        }
    }

    private void getVentesByClientLike() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchBy(10, typePieceJCB.getSelectedIndex(), agencesJCB.getSelectedIndex() + 1, dateDu, dateAu, requeteF.getText())) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ " + typePiece + " PAR CLIENT ]");
        } else {
            JOptionPane.showMessageDialog(this, "Le Client : " + requeteF.getText() + "\nN'existe pas !");
        }
    }

    private void getVentesByCodeClient() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchBy(2, typePieceJCB.getSelectedIndex(), agencesJCB.getSelectedIndex() + 1, dateDu, dateAu)) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ " + typePiece + " PAR CODE CLIENT ]");
        } else {
            JOptionPane.showMessageDialog(this, "Aucune donn\351e ne correspond \340 la requ\352te demand\351e !");
        }
    }

    private void getVentesByCodeClientLike() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchBy(11, typePieceJCB.getSelectedIndex(), agencesJCB.getSelectedIndex() + 1, dateDu, dateAu, requeteF.getText())) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ " + typePiece + " PAR CODE CLIENT ]");
        } else {
            JOptionPane.showMessageDialog(this, "Le Code Client : " + requeteF.getText() + "\nN'existe pas !");
        }
    }

    private void getVentesByNumero() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchBy(3, typePieceJCB.getSelectedIndex(), agencesJCB.getSelectedIndex() + 1, dateDu, dateAu)) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ " + typePiece + " PAR NUMERO ]");
        } else {
            JOptionPane.showMessageDialog(this, "Aucune donn\351e ne correspond \340 la requ\352te demand\351e !");
        }
    }

    private void getVentesByNumeroLike() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchBy(12, typePieceJCB.getSelectedIndex(), agencesJCB.getSelectedIndex() + 1, dateDu, dateAu, requeteF.getText())) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ " + typePiece + " PAR NUMERO ]");
        } else {
            JOptionPane.showMessageDialog(this, "Le Numero : " + requeteF.getText() + "\nN'existe pas !");
        }
    }

    private void getVentesByEcheance() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchBy(7, typePieceJCB.getSelectedIndex(), agencesJCB.getSelectedIndex() + 1, dateDu, dateAu)) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ " + typePiece + " PAR ECHEANCE ]");
        } else {
            JOptionPane.showMessageDialog(this, "Aucune donn\351e ne correspond \340 la requ\352te demand\351e !");
        }
    }

    private void getVentesByResteDu() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchBy(8, typePieceJCB.getSelectedIndex(), agencesJCB.getSelectedIndex() + 1, dateDu, dateAu)) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ " + typePiece + " PAR RESTE DU ]");
        } else {
            JOptionPane.showMessageDialog(this, "Aucune donn\351e ne correspond \340 la requ\352te demand\351e !");
        }
    }

    private void getVentesByCommerciauxId() {
        OLD_SaleSQLList listeVentes = new OLD_SaleSQLList();
        if (listeVentes.searchById(9, typePieceJCB.getSelectedIndex(), agencesJCB.getSelectedIndex() + 1, dateDu, dateAu, Integer.toString(commerciauxId[commerciauxJCB.getSelectedIndex()]))) {
            rangees = listeVentes.getRangees();
            colonnes = listeVentes.getColonnes();
            afficherListe();
            setTitle("LISTE VENTES     [ DU COMMERCIAL " + commerciauxJCB.getSelectedItem() + " ]");
        } else {
            JOptionPane.showMessageDialog(this, "Aucune Vente n'est rattach\351 au Commercial : " + commerciauxJCB.getSelectedItem());
        }
    }

    private void afficherListe() {
        TableModel myModel = new TableModel();
        table = new JTable(myModel);
        tableModele();
        listSelectionModel = table.getSelectionModel();
        listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
        table.setSelectionModel(listSelectionModel);
        defiler.setViewportView(table);
        calculeTotal();
    }

    private void tableModele() {
        TableColumn column = new TableColumn();
        for (int i = 0; i < 7; i++) {
            column = table.getColumnModel().getColumn(i);
            if (i == 0) {
                column.setPreferredWidth(60);
            } else if (i == 1) {
                column.setPreferredWidth(50);
            } else if (i == 2) {
                column.setPreferredWidth(50);
            } else if (i == 3) {
                column.setPreferredWidth(100);
            } else if (i == 4) {
                column.setPreferredWidth(50);
            } else if (i == 5) {
                column.setPreferredWidth(50);
            } else if (i == 6) {
                column.setPreferredWidth(80);
            }
        }

    }

    private void print() {
        String columnName[] = {
            "col1", "col2", "col3", "col4", "col5", "col6", "col7"
        };
        Object data[][] = new Object[rangees.size() / colonnes.size()][7];
        int j = 0;
        for (int i = 0; i < data.length; i++) {
            data[i][0] = (String) rangees.get(j++);
            data[i][1] = (String) rangees.get(j++);
            data[i][2] = (String) rangees.get(j++);
            data[i][3] = (new BigDecimal((String) rangees.get(j++))).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toPlainString();
            data[i][4] = (new BigDecimal((String) rangees.get(j++))).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toPlainString();
            data[i][5] = (new BigDecimal((String) rangees.get(j++))).setScale(param.getMinimumDecimalNumber().intValue(), param.getRoundType().intValue()).toPlainString();
            data[i][6] = (String) rangees.get(j++);
        }

        ListPrint listPrint = new ListPrint(data, columnName, "piecesSaleList", true, param);
    }

    private void printPiece() {
        if (vtId == null) {
            JOptionPane.showMessageDialog(this, "S\351l\351ctionner une ou plusieus lignes !");
        } else {
            if (crud == null) {
                crud = new ObjectCRUD();
            }
            OrderController oCtrl = null;
            PiecesPrint print = null;
            for (int i = 0; i < vtId.length; i++) {
                if (vtId[i] != null) {
                    oCtrl = new OrderController(getOpenPiece(typePieceJCB.getSelectedIndex(), vtId[i]), typePieceJCB.getSelectedIndex(), param);
                    print = new PiecesPrint(oCtrl.getAgency(), oCtrl.getAddress(), oCtrl.getPiece(), oCtrl.getSalesPerson(), oCtrl.getRowsList(), false, param);
                }
            }

        }
    }

    private Object getOpenPiece(int type, String pieceId) {
        switch (type) {
            case 0: // '\0'
                return crud.read(pieceId, 22);

            case 1: // '\001'
                return crud.read(pieceId, 22);

            case 2: // '\002'
                return crud.read(pieceId, 22);

            case 3: // '\003'
                return crud.read(pieceId, 25);

            case 4: // '\004'
                return crud.read(pieceId, 25);

            case 5: // '\005'
                return crud.read(pieceId, 24);

            case 6: // '\006'
                return crud.read(pieceId, 24);
        }
        return null;
    }

    private void selectRequete() {
        switch (typePieceJCB.getSelectedIndex()) {
            case 0: // '\0'
                typePiece = "DEVIS";
                break;

            case 1: // '\001'
                typePiece = "COMMANDE";
                break;

            case 2: // '\002'
                typePiece = "BL";
                break;

            case 3: // '\003'
                typePiece = "FACTURE";
                break;

            case 4: // '\004'
                typePiece = "AVOIR";
                break;
        }
        if (periodeJCB.getSelectedIndex() == 2) {
            dateDu = (String) duF.getSelectedItem();
            dateAu = (String) auF.getSelectedItem();
        }
        if (agencesJCB.getSelectedIndex() == 5) {
            getVentesByClientAllAG();
        } else if (parJR.isSelected()) {
            switch (selectJCB.getSelectedIndex()) {
                default:
                    break;

                case 0: // '\0'
                    if (requeteF.getText().equals("")) {
                        getVentesByClient();
                    } else {
                        getVentesByClientLike();
                    }
                    break;

                case 1: // '\001'
                    if (requeteF.getText().equals("")) {
                        getVentesByCodeClient();
                    } else {
                        getVentesByCodeClientLike();
                    }
                    break;

                case 2: // '\002'
                    if (requeteF.getText().equals("")) {
                        getVentesByNumero();
                    } else {
                        getVentesByNumeroLike();
                    }
                    break;

                case 3: // '\003'
                    if (typePieceJCB.getSelectedIndex() == 3) {
                        getVentesByEcheance();
                    } else {
                        JOptionPane.showMessageDialog(this, "Requ\352te reserv\351e aux Factures !");
                    }
                    break;

                case 4: // '\004'
                    if (typePieceJCB.getSelectedIndex() == 3) {
                        getVentesByResteDu();
                    } else {
                        JOptionPane.showMessageDialog(this, "Requ\352te reserv\351e aux Factures !");
                    }
                    break;
            }
            requeteF.setText("");
        } else if (parCommercialJR.isSelected()) {
            getVentesByCommerciauxId();
        }
    }

    private void calculeTotal() {
        BigDecimal totalUn = new BigDecimal(0);
        BigDecimal totalDeux = new BigDecimal(0);
        BigDecimal totalTrois = new BigDecimal(0);
        if (typePieceJCB.getSelectedIndex() == 3) {
            for (int i = 0; i < table.getRowCount(); i++) {
                totalUn = totalUn.add(new BigDecimal((String) table.getValueAt(i, 5)));
                totalDeux = totalDeux.add(new BigDecimal((String) table.getValueAt(i, 4)));
                totalTrois = totalTrois.add(new BigDecimal((String) table.getValueAt(i, 3)));
            }

            chiffreResteDuJL.setText("TOTAL RESTE DU  " + totalUn.toString());
            chiffreBrutJL.setText("TOTAL HT  " + totalTrois.toString());
        } else {
            for (int i = 0; i < table.getRowCount(); i++) {
                totalUn = totalUn.add(new BigDecimal((String) table.getValueAt(i, 3)));
                totalDeux = totalDeux.add(new BigDecimal((String) table.getValueAt(i, 4)));
            }

            chiffreBrutJL.setText("TOTAL HT  " + totalUn.toString());
            chiffreResteDuJL.setText("");
        }
        chiffreNetJL.setText("TOTAL TTC  " + totalDeux.toString());
    }

    public void setParameter(Parameter param) {
        this.param = param;
    }
    private JComboBox agencesJCB;
    private DateComboBox auF;
    private JLabel auL;
    private ButtonGroup buttonGroup1;
    private JLabel chiffreBrutJL;
    private JLabel chiffreNetJL;
    private JLabel chiffreResteDuJL;
    private JComboBox commerciauxJCB;
    private JPanel dataP;
    private JScrollPane defiler;
    private DateComboBox duF;
    private JPanel fieldSelectionP;
    private JMenuItem helpMI;
    private IconBar iconBar1;
    private JMenuBar jMenuBar1;
    private JSeparator jSeparator1;
    private JPanel mainP;
    private JMenuItem modifyMI;
    private JMenuItem newMI;
    private JMenuItem openMI;
    private JMenu optionsM;
    private JRadioButton parCommercialJR;
    private JRadioButton parJR;
    private JComboBox periodeJCB;
    private JMenuItem printMI;
    private JMenuItem removeMI;
    private JTextField requeteF;
    private JMenuItem saveMI;
    private JComboBox selectJCB;
    private JPanel selectionP;
    private JComboBox typePieceJCB;
    private JButton validerB;
    private JPanel voletBas;
    private ListSelectionModel listSelectionModel;
    private Vector rangees;
    private Vector colonnes;
    private JTable table;
    private PrintSelectorGUI ps;
    private Dimension screen;
    private int commerciauxId[];
    private int dateDuAu;
    private final int CLIENT = 0;
    private final int CODE_CLIENT = 1;
    private final int NUMERO = 2;
    private final int ECHEANCE = 3;
    private final int RESTE_DU = 4;
    private final int AGENCE_UN = 0;
    private final int AGENCE_DEUX = 1;
    private final int AGENCE_TROIS = 2;
    private final int AGENCE_QUATRE = 3;
    private final int AGENCE_CINQ = 4;
    private final int AGENCE_ALL = 5;
    private final int DEVIS = 0;
    private final int COMMANDE = 1;
    private final int BL = 2;
    private final int FACTURE = 3;
    private final int AVOIR = 4;
    private final int MOIS_EN_COURS = 0;
    private final int ANNEE_EN_COURS = 1;
    private final int DU = 2;
    private final int TOUS = 3;
    private Parameter param;
    private String dateDu;
    private String dateAu;
    private String typePiece;
    private String vtId[];
    private ObjectCRUD crud;
}
